
package com.coolgor.coldot.service.stock.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.stock.StockInfo;
import com.coolgor.coldot.dao.stock.StockInfoDao;
import com.coolgor.coldot.dto.stock.StockInfoExecution;
import com.coolgor.coldot.enums.stock.StockInfoOperationStateEnum;
import com.coolgor.coldot.exceptions.stock.StockInfoOperationException;
import com.coolgor.coldot.service.stock.StockInfoService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class StockInfoServiceImpl implements StockInfoService {

	private static Logger log = LoggerFactory.getLogger(StockInfoServiceImpl.class);

	@Autowired
	StockInfoDao stockInfoDao;

	@Override
	public StockInfoExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL_ID);
		}
		StockInfo stockInfo = stockInfoDao.queryByID(recordID);
		if (stockInfo != null) {
			return new StockInfoExecution(StockInfoOperationStateEnum.SUCCESS, stockInfo);
		} else {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL);
		}
	}

	@Override
	public StockInfoExecution getList(StockInfo stockInfoCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<StockInfo> list = stockInfoDao.queryList(stockInfoCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = stockInfoDao.queryCount(stockInfoCondition);
		StockInfoExecution result = new StockInfoExecution();
		if (list != null) {
			result.setState(StockInfoOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(StockInfoOperationStateEnum.SUCCESS.getStateInfo());
			result.setStockInfoList(list);
			result.setCount(count);
		} else {
			result.setState(StockInfoOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public StockInfoExecution add(StockInfo stockInfo) throws StockInfoOperationException {
		// 空值判断
		if (stockInfo == null) {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			stockInfo.setCreateTime(new Date());
			stockInfo.setLastEditTime(new Date());
			// 执行插入数据操作 
			effectedNum = stockInfoDao.insert(stockInfo);
			if (effectedNum <= 0) {
				throw new StockInfoOperationException("StockInfo信息插入失败");
			}
		} catch (Exception e) {
			throw new StockInfoOperationException("Add stockInfo error:" + e.getMessage());
		}
		return new StockInfoExecution(StockInfoOperationStateEnum.SUCCESS, stockInfo);
	}

	@Override
	@Transactional
	public StockInfoExecution modify(StockInfo stockInfo) throws StockInfoOperationException {
		// 空值判断
		if (stockInfo == null) {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 把记录的最后更新时间设置为当前时间 
			stockInfo.setLastEditTime(new Date());
			// 执行修改更新操作 
			effectedNum = stockInfoDao.update(stockInfo);
			if (effectedNum <= 0) {
				throw new StockInfoOperationException("StockInfo信息更新失败");
			}
		} catch (Exception e) {
			throw new StockInfoOperationException("Update stockInfo error:" + e.getMessage());
		}
		return new StockInfoExecution(StockInfoOperationStateEnum.SUCCESS, stockInfo);
	}

	@Override
	@Transactional
	public StockInfoExecution remove(Long recordID) throws StockInfoOperationException{
		// 空值判断
		if (recordID == null) {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockInfoDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new StockInfoOperationException("StockInfo单条信息删除失败");
			}
		} catch (Exception e) {
			throw new StockInfoOperationException("Delete stockInfo error:" + e.getMessage());
		}
		return new StockInfoExecution(StockInfoOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public StockInfoExecution remove(StockInfo stockInfo) throws StockInfoOperationException{
		// 空值判断
		if (stockInfo == null) {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockInfoDao.delete(stockInfo.getRecordID());
			if (effectedNum <= 0) {
				throw new StockInfoOperationException("StockInfo单条信息删除失败");
			}
		} catch (Exception e) {
			throw new StockInfoOperationException("Delete stockInfo error:" + e.getMessage());
		}
		return new StockInfoExecution(StockInfoOperationStateEnum.SUCCESS, stockInfo);
	}

	@Override
	@Transactional
	public StockInfoExecution removeBatchByID(List<Long> recordIDList) throws StockInfoOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockInfoDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new StockInfoOperationException("StockInfo多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new StockInfoOperationException("Batch Delete stockInfo error:" + e.getMessage());
		}
		StockInfoExecution exe = new StockInfoExecution(StockInfoOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public StockInfoExecution removeBatch(List<StockInfo> stockInfoList) throws StockInfoOperationException{
		// 空值判断
		if (stockInfoList == null) {
			return new StockInfoExecution(StockInfoOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<stockInfoList.size(); i++) {
			recordIDList.add(stockInfoList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockInfoDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new StockInfoOperationException("StockInfo多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new StockInfoOperationException("Batch Delete stockInfo error:" + e.getMessage());
		}
		StockInfoExecution exe = new StockInfoExecution(StockInfoOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
